简体   繁体   English

使用 pandas dataframe 中的值作为另一个的列名

[英]Using values in a pandas dataframe as column names for another

This is my first dataframe.这是我的第一个 dataframe。

Date        0   1   2   3   
2003-01-31  CA  KY  ID  CO
2003-02-28  CA  KY  HI  CO 
2003-03-31  CA  KY  CO  HI 

This is my second dataframe.这是我的第二个 dataframe。

Date        CA  KY  ID  CO  HI                                            
2003-01-31   5   3   4   5   1 
2003-02-28   2   7   8   4   5  
2003-03-31   6   3   9   3   5 

How do I get this dataframe to print as output?如何让这个 dataframe 打印为 output?

Date         0   1   2   3                                                
2003-01-31   5   3   4   5
2003-02-28   2   7   5   4
2003-03-31   6   3   3   5

I am wondering if there is a way to use the whole dataframe as an index to another instead of having to loop through all the dates/columns.我想知道是否有一种方法可以使用整个 dataframe 作为另一个索引,而不必遍历所有日期/列。

You can use df.lookup with df.apply here.您可以在此处将df.lookupdf.apply一起使用。

# If `Date` is not index.
# df1.set_index('Date')
#              0   1   2   3
# Date
# 2003-01-31  CA  KY  ID  CO
# 2003-02-28  CA  KY  HI  CO
# 2003-03-31  CA  KY  CO  HI

# df2.set_index('Date')
#             CA  KY  ID  CO  HI
# Date
# 2003-01-31   5   3   4   5   1
# 2003-02-28   2   7   8   4   5
# 2003-03-31   6   3   9   3   5

def f(x):
    return df2.lookup(x.index, x)

df1.apply(f)
# df1.apply(lambda x: df2.lookup(x.index, x)

            0  1  2  3
Date
2003-01-31  5  3  4  5
2003-02-28  2  7  5  4
2003-03-31  6  3  3  5

This will print the values of dataframe df and the column name of dataframe df1.这将打印 dataframe df 的值和 dataframe df1 的列名。

print(df.rename(columns={i:j for i,j in zip(df.columns.tolist(),df1.columns.tolist())}))

if you want to make the changes permanent, add the parameter inplace=TRUE如果要使更改永久化,请添加参数inplace=TRUE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用另一个 pandas 数据框的列填充 na 值,但使用列索引,而不是名称 - Fill na values in one pandas dataframe's column using another, but using column indices, not names 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? Gapfill pandas dataframe 在另一个 dataframe 中使用同一列中的值 - Gapfill pandas dataframe using values from same column in another dataframe 使用另一个 dataFrame 更改 Pandas dataFrame 中的列中的值 - Changing values in a column in a Pandas dataFrame by using another dataFrame 如果另一个 dataframe 中没有具有相同索引和列名的值,如何清除 pandas 中 dataframe 的列中的值 - How to clear values in columns of a dataframe in pandas if there are no values in another dataframe with the same index an column names 使用 Pandas 将特定列值替换为另一个数据框列值 - Replace specific column values with another dataframe column value using Pandas 根据 pandas dataframe 中的另一列获取名称 - Get names based on another column in pandas dataframe Pandas将数据帧值转换为列名 - Pandas convert dataframe values to column names 熊猫数据框获取具有单元格中值的列名称 - Pandas dataframe get column names with values in cell Pandas 数据框用列名映射行值 - Pandas dataframe map rows values with column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM