I have a pandas Series as se:
wind yes
humidity high
weather sunny
temp hot
conclusion bad
Name: 1, dtype: object
And I want to convert it into a dataframe like:
| weather| temp | humidity | wind | conclusion
0 | sunny | hot | high | yes | bad
I've tried
pd.DataFrame(se)
but it turns out to be something else
wind yes
humidity high
weather sunny
temp hot
conclusion bad
CodePudding user response:
Use to_frame and transpose your new dataframe:
df = sr.to_frame(0).T
print(df)
# Output:
wind humidity weather temp conclusion
0 yes high sunny hot bad
Setup
data = {'wind': 'yes',
'humidity': 'high',
'weather': 'sunny',
'temp': 'hot',
'conclusion': 'bad'}
sr = pd.Series(data, name=1)
print(sr)
# Output
wind yes
humidity high
weather sunny
temp hot
conclusion bad
Name: 1, dtype: object
CodePudding user response:
Use this, it might help :)
se.to_frame()
CodePudding user response:
More than likely you got this series buy selecting a single row of your originaldataframe using .loc[0] or .iloc[0]. Try using double brackets to select a single row from a dataframe as a dataframe instead of converting to a series.
df.iloc[[0]]
or
df.loc[[0]]
MVCE:
df = pd.DataFrame({'Col1':[*'ABC'],
'Col2':'Alpha Bravo Charlie'.split(' '),
'Col3': [1,2,3]})
df.loc[0]
Outputs a pd.Series:
Col1 A
Col2 Alpha
Col3 1
Name: 0, dtype: object
Now try double brackets:
df.loc[[0]]
Outputs a signle row pd.Dataframe:
Col1 Col2 Col3
0 A Alpha 1
And, likewise with .iloc.
