Pandas 将字典连接到数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36366036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas concat dictionary to dataframe
提问by Lukasz
I have an existing dataframe and I'm trying to concatenate a dictionary where the length of the dictionary is different from the dataframe
我有一个现有的数据框,我正在尝试连接一个字典,其中字典的长度与数据框不同
>>> df
A B C
0 0.46324 0.32425 0.42194
1 0.10596 0.35910 0.21004
2 0.69209 0.12951 0.50186
3 0.04901 0.31203 0.11035
4 0.43104 0.62413 0.20567
5 0.43412 0.13720 0.11052
6 0.14512 0.10532 0.05310
and
和
test = {"One": [0.23413, 0.19235, 0.51221], "Two": [0.01293, 0.12235, 0.63291]}
I'm trying to add test
to df
, while changing the keys to "D"
and "C"
and I've had a look at
我正在尝试添加test
到df
,同时将键更改为"D"
and"C"
并且我已经查看了
http://pandas.pydata.org/pandas-docs/stable/merging.htmland http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html
http://pandas.pydata.org/pandas-docs/stable/merging.html和 http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html
which indicates that I should be able to concatenate the dictionary to the dataframe
这表明我应该能够将字典连接到数据框
I've tried:
我试过了:
pd.concat([df, test], axis=1, ignore_index=True, keys=["D", "E"])
pd.concat([df, test], axis=1, ignore_index=True)
but I'm not having any luck, the result I'm trying to achieve is
但我没有任何运气,我想要达到的结果是
df
A B C D E
0 0.46324 0.32425 0.42194 0.23413 0.01293
1 0.10596 0.35910 0.21004 0.19235 0.12235
2 0.69209 0.12951 0.50186 0.51221 0.63291
3 0.04901 0.31203 0.11035 NaN NaN
4 0.43104 0.62413 0.20567 NaN NaN
5 0.43412 0.13720 0.11052 NaN NaN
6 0.14512 0.10532 0.05310 NaN NaN
回答by Sergey Bushmanov
The only way you can do that is with:
您可以这样做的唯一方法是:
df.join(pd.DataFrame(test).rename(columns={'One':'D','Two':'E'}))
A B C D E
0 0.46324 0.32425 0.42194 0.23413 0.01293
1 0.10596 0.35910 0.21004 0.19235 0.12235
2 0.69209 0.12951 0.50186 0.51221 0.63291
3 0.04901 0.31203 0.11035 NaN NaN
4 0.43104 0.62413 0.20567 NaN NaN
5 0.43412 0.13720 0.11052 NaN NaN
6 0.14512 0.10532 0.05310 NaN NaN
because as @Alexander mentioned correctly the number of rows being concatenated should match. Otherwise, as in your case, missing rows will be filled with NaN
因为正如@Alexander 正确提到的,被连接的行数应该匹配。否则,在你的情况下,缺失的行将被填充NaN
回答by Alexander
Assuming you want to add them as rows:
假设您想将它们添加为行:
>>> pd.concat([df, pd.DataFrame(test.values(), columns=df.columns)], ignore_index=True)
A B C
0 0.46324 0.32425 0.42194
1 0.10596 0.35910 0.21004
2 0.69209 0.12951 0.50186
3 0.04901 0.31203 0.11035
4 0.43104 0.62413 0.20567
5 0.43412 0.13720 0.11052
6 0.14512 0.10532 0.05310
7 0.01293 0.12235 0.63291
8 0.23413 0.19235 0.51221
If added as new columns:
如果添加为新列:
df_new = pd.concat([df, pd.DataFrame(test.values()).T], ignore_index=True, axis=1)
df_new.columns = \
df.columns.tolist() + [{'One': 'D', 'Two': 'E'}.get(k) for k in test.keys()]
>>> df_new
A B C E D
0 0.46324 0.32425 0.42194 0.01293 0.23413
1 0.10596 0.35910 0.21004 0.12235 0.19235
2 0.69209 0.12951 0.50186 0.63291 0.51221
3 0.04901 0.31203 0.11035 NaN NaN
4 0.43104 0.62413 0.20567 NaN NaN
5 0.43412 0.13720 0.11052 NaN NaN
6 0.14512 0.10532 0.05310 NaN NaN
Order is not guaranteed in dictionaries (e.g. test
), so the new column names actually need to be mapped to the keys.
字典(例如test
)中不能保证顺序,因此新的列名实际上需要映射到键。