有两个数据帧,一个有很少的信息(df1),另一个有所有数据(df2)。我想在df1中的新列中创建,找到Total2值并根据Names相应地填充新列。请注意,df1中可见的名称将始终在名称df2中找到匹配项。我想知道Pandas中是否有一些功能已经这样做了?我的最终目标是创建一个条形图。
alldatapath = "all_data.csv"
filteredpath = "filtered.csv"
import pandas as pd
df1 = pd.read_csv(
filteredpath, # file name
sep=',', # column separator
quotechar='"', # quoting character
na_values="NA", # fill missing values with 0
usecols=[0,1], # columns to use
decimal='.') # symbol for decimals
df2 = pd.read_csv(
alldatapath, # file name
sep=',', # column separator
quotechar='"', # quoting character
na_values="NA", # fill missing values with 0
usecols=[0,1], # columns to use
decimal='.') # symbol for decimals
df1 = df1.head(5) #trim to top 5
print(df1)
print(df2)
输出(df1):
Name Total
0 Accounting 3
1 Reporting 1
2 Finance 1
3 Audit 1
4 Template 2
输出(df2):
Name Total2
0 Reporting 100
1 Accounting 120
2 Finance 400
3 Audit 500
4 Information 50
5 Template 1200
6 KnowHow 2000
最终输出(df1)应该是这样的:
Name Total Total2(new column)
0 Accounting 3 120
1 Reporting 1 100
2 Finance 1 400
3 Audit 1 500
4 Template 2 1200
答案 0 :(得分:5)
首先需要Series
df1['Total2'] = df1['Name'].map(df2.set_index('Name')['Total2'])
print (df1)
Name Total Total2
0 Accounting 3 120
1 Reporting 1 100
2 Finance 1 400
3 Audit 1 500
4 Template 2 1200
新列:
df1.set_index('Name').plot.bar()
val headers_10 = Map("Content-Type" -> """application/json""", "API-KEY" -> "your_api_key", "Authorization Bearer" -> "auth_bearer")