Pandas dataframe drop columns with no header
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.
There may or may not be data in the column.
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
df.dropna(axis='columns', inplace=True)
Doesn't do it as it looks at the data in the column.
Wanted output
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])
Thanks in advance for the replies.
python pandas dataframe indexing nan
add a comment |Â
up vote
6
down vote
favorite
What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.
There may or may not be data in the column.
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
df.dropna(axis='columns', inplace=True)
Doesn't do it as it looks at the data in the column.
Wanted output
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])
Thanks in advance for the replies.
python pandas dataframe indexing nan
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.
There may or may not be data in the column.
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
df.dropna(axis='columns', inplace=True)
Doesn't do it as it looks at the data in the column.
Wanted output
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])
Thanks in advance for the replies.
python pandas dataframe indexing nan
What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.
There may or may not be data in the column.
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
df.dropna(axis='columns', inplace=True)
Doesn't do it as it looks at the data in the column.
Wanted output
df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])
Thanks in advance for the replies.
python pandas dataframe indexing nan
python pandas dataframe indexing nan
edited 1 hour ago
jpp
67.2k173984
67.2k173984
asked 1 hour ago
Joylove
13210
13210
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
Simply try this
df.drop(np.nan, axis=1, inplace=True)
why passing as a list?
â pyd
1 hour ago
1
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
add a comment |Â
up vote
1
down vote
You can use pd.Index.dropna
:
df = df[df.columns.dropna()]
print(df)
col1 col2
0 1.0 4
1 2.0 5
2 NaN 6
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Simply try this
df.drop(np.nan, axis=1, inplace=True)
why passing as a list?
â pyd
1 hour ago
1
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
add a comment |Â
up vote
4
down vote
Simply try this
df.drop(np.nan, axis=1, inplace=True)
why passing as a list?
â pyd
1 hour ago
1
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Simply try this
df.drop(np.nan, axis=1, inplace=True)
Simply try this
df.drop(np.nan, axis=1, inplace=True)
edited 23 mins ago
answered 1 hour ago
bakka
3688
3688
why passing as a list?
â pyd
1 hour ago
1
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
add a comment |Â
why passing as a list?
â pyd
1 hour ago
1
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
why passing as a list?
â pyd
1 hour ago
why passing as a list?
â pyd
1 hour ago
1
1
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
habit :D. doesn't have to. thanks for making perfect!
â bakka
42 mins ago
add a comment |Â
up vote
1
down vote
You can use pd.Index.dropna
:
df = df[df.columns.dropna()]
print(df)
col1 col2
0 1.0 4
1 2.0 5
2 NaN 6
add a comment |Â
up vote
1
down vote
You can use pd.Index.dropna
:
df = df[df.columns.dropna()]
print(df)
col1 col2
0 1.0 4
1 2.0 5
2 NaN 6
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use pd.Index.dropna
:
df = df[df.columns.dropna()]
print(df)
col1 col2
0 1.0 4
1 2.0 5
2 NaN 6
You can use pd.Index.dropna
:
df = df[df.columns.dropna()]
print(df)
col1 col2
0 1.0 4
1 2.0 5
2 NaN 6
answered 1 hour ago
jpp
67.2k173984
67.2k173984
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52568437%2fpandas-dataframe-drop-columns-with-no-header%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password