Pandas Column Access W/Column Names Containing Spaces

Pandas column access w/column names containing spaces

I think the default way is to use the bracket method instead of the dot notation.

import pandas as pd

df1 = pd.DataFrame({
'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'dat a1': range(7)
})

df1['dat a1']

The other methods, like exposing it as an attribute are more for convenience.

How to search in a pandas dataframe column with the space in the column name

It's better practice to use the square bracket notation:

df["Date Time"].values

Which does exactly the same thing

python dataframe query with spaces in column name

I wouldn't use query function. I would use the square bracket notation:

dfResult = dfResult[dfResult['Column A'].isin(ExcludeData)]

How to use `pd.Series.str.contains()` when whitespace present in column name?

You access your columny also by indexing into your data frame and not using the dot notation. So just do

first_match = df["Tunnel ID"].str.contains('ic').idxmax()

and you should be good to go

Access rows with string in dataframe column, which contain 2 or more spaces between words using Pandas

Use str.split to split company names into words and count the length of the list then select right rows:

data = pd.DataFrame({'Company Name': ['American Telephone and Telegraph', 
'America Online',
'Capsule Computer',
'International Business MachinesHP']})

required_data1 = data[data['Company Name'].str.split(r'\s+').str.len().ge(3)]
print(required_data1)

# Output
Company Name
0 American Telephone and Telegraph
3 International Business MachinesHP

Pandas query function not working with spaces in column names

From pandas 0.25 onward you will be able to escape column names with backticks so you can do

a.query('`a b` == 5') 

Querying Pandas DataFrame with column name that contains a space or using the drop method with a column name that contains a space

If I understood correctly your issue, maybe you can just apply a filter like:

df = df[df['Sale Item'] != 'item1']

which returns:

         Date    price Sale Item
1 2012-06-12 1610.02 item2
2 2012-06-13 1618.07 item3
3 2012-06-14 1624.40 item4
4 2012-06-15 1626.15 item5
5 2012-06-16 1626.15 item6
6 2012-06-17 1626.15 item7

How to Reference a Pandas Column that has a dot in the name

Using the replace command to remove offensive characters works ok: Removing space from dataframe columns in pandas.



Related Topics



Leave a reply



Submit